package countCharacters;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int countCharacters(String[] words, String chars) {
        Map<Character,Integer> map = new HashMap<>();
        for (char c: chars.toCharArray()) {
            map.put(c,map.getOrDefault(c,0)+1);
        }
        int ans = 0;
        for (String s: words) {
            Map<Character,Integer> map1 = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                map1.put(s.charAt(i),map1.getOrDefault(s.charAt(i),0)+1);
            }
            boolean can = true;
            for (char key: map1.keySet()) {
                if(map1.get(key)>map.getOrDefault(key,0)){
                    can = false;
                    break;
                }
            }
            if(can){
                ans+=s.length();
            }
        }
        return ans;
    }
}
